package ch03.demo.proxy;

import ch03.demo.service.impl.RealBuyer;
import net.sf.cglib.proxy.Enhancer;
import org.apache.log4j.Logger;
import org.junit.Test;

public class IntermediaryCglibProxyFactory {
    private Logger logger=Logger.getLogger(IntermediaryCglibProxyFactory.class);
    private static IntermediaryMethodIntercerpor callback=new IntermediaryMethodIntercerpor();

    /**
     * 工厂方法
     * @param target
     * @return
     * @param <T>
     */
    public static <T> T create(Class<T> target){
        Enhancer enhancer=new Enhancer();
        enhancer.setCallback(callback);
        enhancer.setSuperclass(target);
        return (T)enhancer.create();
    }
    @Test
    public void haverlookCglibProxy()throws Exception{
        RealBuyer buyer=IntermediaryCglibProxyFactory.create(RealBuyer.class);
        String result=buyer.havealook();
        logger.debug(result);
    }
}
